﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Reflection;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrainTechLLC.ThreadSafeObjects;
using System.Runtime.Serialization;
using Mud.EmlenMudMain;

namespace BrainTechLLC
{
    [Serializable]
    [DataContract]
    public class ItemTemplate : Template
    {
        public ItemTemplate()
        {
            _typeName = "BrainTechLLC.Item";
        }

        public static ItemTemplate FindItemTemplateByVnum(int vnum, Dictionary<int, ItemTemplate> dict)
        {
            ItemTemplate t;

            if (dict.TryGetValue(vnum, out t))
                return t;

            return null;            
        }

        public static Dictionary<int, ItemTemplate> ReadItemTemplatesByVnum()
        {
            Dictionary<int, ItemTemplate> result = new Dictionary<int, ItemTemplate>();
            var items = Everything.FindAllThingsOfType<ItemTemplate>();
            foreach (var loc in items)
            {
                int vnum = loc.ThingProperties.GetArgument<int>("Vnum");
                
                if (vnum > 0)
                    result.Add(vnum, loc);                    
            }
            return result;
        }

        public override void RegisterTemplate()
        {
            base.RegisterTemplate();
        }

        public override void AddDefaults(IFromTemplate o)
        {
            base.AddDefaults(o);
            Item item = o as Item;
            
            // TODO - item setup
        }

        [DataMember]
        public List<string> _actionDescription;

        public List<string> ActionDescription
        {
            get
            {
                if (_actionDescription == null)
                    Interlocked.CompareExchange<List<string>>(ref _actionDescription, new List<string>(), null);

                return _actionDescription;
            }
            set
            {
                if (!_actionDescription.AreEqual(value))
                {
                    OnPropertyChanging("ActionDescription", _actionDescription, value);
                    _actionDescription = value;
                    OnPropertyChanged("ActionDescription", _actionDescription, value);
                }
            }
        }

        [DataMember]
        public string _areaName;

        public string AreaName
        {
            get { return _areaName; }
            set
            {
                if (!_areaName.AreEqual(value))
                {
                    OnPropertyChanging("AreaName", _areaName, value);
                    _areaName = value;
                    OnPropertyChanged("AreaName", _areaName, value);
                }
            }
        }

        [DataMember]
        public int _itemCount;

        public int ItemCount
        {
            get { return _itemCount; }
            set
            {
                if (!_itemCount.AreEqual(value))
                {
                    OnPropertyChanging("ItemCount", _itemCount, value);
                    _itemCount = value;
                    OnPropertyChanged("ItemCount", _itemCount, value);
                }
            }
        }

        [DataMember]
        public List<EffectData> _tempEffects;

        public List<EffectData> TempEffects
        {
            get
            {
                if (_tempEffects == null)
                    Interlocked.CompareExchange<List<EffectData>>(ref _tempEffects, new List<EffectData>(), null);

                return _tempEffects;
            }
            set
            {
                if (!_tempEffects.AreEqual(value))
                {
                    OnPropertyChanging("TempEffects", _tempEffects, value);
                    _tempEffects = value;
                    OnPropertyChanged("TempEffects", _tempEffects, value);
                }
            }
        }

        [DataMember]
        public List<ExtraDescriptionData> _extraDescriptions;

        public List<ExtraDescriptionData> ExtraDescriptions
        {
            get
            {
                if (_extraDescriptions == null)
                    Interlocked.CompareExchange<List<ExtraDescriptionData>>(ref _extraDescriptions, new List<ExtraDescriptionData>(), null);

                return _extraDescriptions;
            }
            set
            {
                if (!_extraDescriptions.AreEqual(value))
                {
                    OnPropertyChanging("ExtraDescriptions", _extraDescriptions, value);
                    _extraDescriptions = value;
                    OnPropertyChanged("ExtraDescriptions", _extraDescriptions, value);
                }
            }
        }
    }
}
